/**
 * 该文件用于初始化地图时，景点的加载
 */

import { fetchAllBriefScenic, fetchScenicDetail } from '@/services/api/scenic'
import type { BriefScenic, ScenicDetail } from '@/utils/dataType/ScenicModel'
import { defineStore } from 'pinia'

export const useScenicMapManageStore = defineStore('scenic', {
  state: () => ({
    briefScenicArray: [] as BriefScenic[],
    specificScenic: null as ScenicDetail | null,
    showCard: false,
    loading: false,
  }),
  actions: {
    // changeShowCard() {
    //   this.showSpecific = !this.showSpecific //取反操作
    // },
    async getAllScenicInfor() {
      console.log('从后端加载地图全部景点信息')
      try {
        const res = await fetchAllBriefScenic()
        this.briefScenicArray = res.data
        // if (res.status == 200 && res.data.code == 1) {
        //   console.log(res.data.data)
        //   this.briefScenicArray = res.data.data
        //   console.log('加载完成')
        //   console.log(this.briefScenicArray)
        // } else {
        //   ElMessage.error('出错了' + res.data.msg)
        // }
      } catch (error) {
        console.error('[获取地图景点信息失败]:', error)
      }
    },
    async getScenicDetail(scenicId) {
      this.loading = true
      this.showCard = true
      try {
        const res = await fetchScenicDetail({ scenicId })
        this.specificScenic = res.data
        // if (res.status == 200 && res.data.code == 1) {
        //   this.specificScenic = res.data.data
        // } else {
        //   ElMessage.error('出错了' + res.data.msg)
        // }
      } catch (error) {
        console.error('获取详景点信息失败:', error)
      } finally {
        this.loading = false
      }
    },
    closeCard() {
      this.specificScenic = null
      this.showCard = false
    },
  },
})
